<?php
include_once('top.php');
$projectID = $_GET['project'];
$project=fetch_only("SELECT Title , Description, P_id FROM PROJECT WHERE P_id='$projectID';");
$manager = fetch_only("SELECT First_name , Last_name
FROM USER RIGHT JOIN (
SELECT Manager
FROM PROJECT
WHERE P_id=$projectID
) as P ON USER.Email=P.Manager;");
//$allMilestones=fetch_all("SELECT MILESTONE.Project,MILESTONE.Due_date,MILESTONE.Name, MILESTONE.Open,COUNT(TASK.Name) as allTasks, SUM(TASK.Open) as openTasks FROM MILESTONE RIGHT JOIN TASK ON (MILESTONE.Name=TASK.Milestone AND MILESTONE.Project=TASK.Project) WHERE MILESTONE.Project=$projectID GROUP BY (MILESTONE.Name)  ORDER BY MILESTONE.Due_date;");
$m=fetch_all("SELECT MILESTONE.Name,MILESTONE.Project,MILESTONE.Due_date,MILESTONE.Open,INFO.allTasks,INFO.openTasks FROM MILESTONE LEFT JOIN (SELECT MILESTONE.Project,MILESTONE.Name,COUNT(TASK.Name) as allTasks, SUM(TASK.Open) as openTasks FROM MILESTONE RIGHT JOIN TASK ON (MILESTONE.Name=TASK.Milestone AND MILESTONE.Project=TASK.Project) WHERE MILESTONE.Project=$projectID GROUP BY (MILESTONE.Name) ORDER BY MILESTONE.Due_date) AS INFO ON (MILESTONE.Project=INFO.Project AND MILESTONE.Name=INFO.Name) WHERE MILESTONE.Project=$projectID ORDER BY MILESTONE.Due_date ASC;");

$allMilestones = Array();

foreach ($m as $milestone) {
	
	if ($milestone['Open'] == '0') {
		array_push($allMilestones, $milestone);
	}
}

foreach ($m as $milestone) {
	if ($milestone['Open'] == '1') {
		array_push($allMilestones, $milestone);
	}
}
?>
<html>
<head>
	<title>GT Project Manager - Project Milestones</title>
	<link rel="stylesheet" type="text/css" href="main.css" />
	<link rel="stylesheet" type="text/css" href="inner.css" />
</head>
<body>
	<table cellspacing="0" cellpadding="0" id="main">
		<tr><td id="title">
			Project Milestones
			<div class="nav">
				<a href="login.php">Log Out</a><br />
				<?php echo $link ?><br />
				<a href="project_info.php?project=<?php echo $project['P_id']; ?>">Project Info</a>
			</div>
		</td></tr>
		<tr>
			<td>
				<table class="details" cellspacing="0" cellpadding="0">
					<tr>
						<th>Project</th>
						<td><?php echo "{$project['Title']} (ID: {$project['P_id']})" ?></td>
					</tr>
					<tr>
						<th>Project Manager</th>
						<td><?php echo $manager['First_name'] . ' ' . $manager['Last_name'];?></td>
					</tr>
					<tr>
						<th>Description</th>
						<td class="description">
							<?php echo $project['Description'];?>
						</td>
					</tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="subsection summary" cellspacing="0" cellpadding="0">
					<tr><td class="title" colspan="4">
						Milestones
						<hr size="1" />
						<div class="link">
							<a href="milestone_info.php?project=<?php echo $projectID; ?>">New Milestone</a>
						</div>
					</td></tr>
					<tr>
						<th>Status</th>
						<th>Name</th>
						<th>Total Tasks</th>
						<th>Active Tasks</th>
					</tr>
					<?php
					for ($i=0; $i<count($allMilestones);$i++) {
					echo "<tr>";
					if (strtotime($allMilestones[$i]['Due_date'])<time() && $allMilestones[$i]['Open'] == '1') echo "<tr class='late'><td>Overdue since " . date("n/j/Y",strtotime($allMilestones[$i]['Due_date'])) . "</td>";
					elseif ($allMilestones[$i]['Open'] == '0') echo "<tr><td>Complete</td>";
					else echo "<tr><td>Active until " . date("n/j/Y",strtotime($allMilestones[$i]['Due_date'])) . "</td>";
					echo "<td><a href='milestone_info.php?project={$allMilestones[$i]['Project']}&milestone={$allMilestones[$i]['Name']}'>{$allMilestones[$i]['Name']}</a></td>
						<td align='center'>{$allMilestones[$i]['allTasks']}</td>
						<td align='center'>{$allMilestones[$i]['openTasks']}</td>
					</tr>";
					}
					?>
				</table>
			</td>
		</tr>
		<tr>
			<td class="button_bar">
				<input type="submit" value="Save" />
			</td>
		</tr>
	</table>
</body>
</html>

